﻿Public Class frmMRol

    Public arrConsulta(3) As String
    Private arrCampos() As String = {"rol_id", "rol_nombre", "rol_descri", "rol_estado"}

    Private Sub frmMRol_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        mdiPrincipal.tsmiRol.Enabled = True
    End Sub

    Private Sub frmRol_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
        refreshList()
    End Sub

    Public Sub refreshList()
        Dim dstRecord As New DataSet
        Dim strCondicion As String = ""
        dstRecord.Tables.Add("Rol")

        For i As Integer = 0 To 3
            If (Me.arrConsulta(i) <> "") Then
                strCondicion &= If(strCondicion <> "", " AND ", "") & If(i = 0, Me.arrCampos(i) & " = '" & LCase(Me.arrConsulta(i)) & "'", "LOWER(" & Me.arrCampos(i) & "::text) LIKE '%" & LCase(Me.arrConsulta(i)) & "%'")
            End If
        Next

        mdlCRUD.loadRecord(dstRecord, "Rol", "rol", strCondicion, Nothing)
        Me.dgvRol.DataSource = dstRecord.Tables("Rol")

        Me.dgvRol.Columns(0).HeaderText = "Codigo"
        Me.dgvRol.Columns(1).HeaderText = "Nombre."
        Me.dgvRol.Columns(2).HeaderText = "Descripción"
        Me.dgvRol.Columns(3).HeaderText = "Estado"

        Me.dgvRol.Columns(0).Width = 60
        Me.dgvRol.Columns(1).Width = 200
        Me.dgvRol.Columns(2).Width = 200
        Me.dgvRol.Columns(3).Width = 80
    End Sub

    Private Sub tsbEditar_Click(ByVal sender As Object, ByVal e As EventArgs) Handles tsbEditar.Click
        Dim strId As String = Me.dgvRol.Rows(Me.dgvRol.CurrentRow.Index).Cells(0).Value.ToString()

        If (strId <> "") Then
            Me.tsbEditar.Enabled = False
            renderForm("Editar - Rol", 3)
        Else
            MsgBox("Debe seleccionar un registro", MsgBoxStyle.Information, "Rol")
        End If
    End Sub

    Private Sub tsbNuevo_Click(ByVal sender As Object, ByVal e As EventArgs) Handles tsbNuevo.Click
        Me.tsbNuevo.Enabled = False
        Me.renderForm("Nuevo - Rol", 1)
    End Sub

    Private Sub tsbRefresh_Click(ByVal sender As Object, ByVal e As EventArgs) Handles tsbRefresh.Click
        arrConsulta(0) = ""
        arrConsulta(1) = ""
        arrConsulta(2) = ""
        arrConsulta(3) = ""
        Me.refreshList()

    End Sub

    Private Sub renderForm(ByVal strTitle As String, ByVal intMode As Integer)
        Dim frmRol As New frmRol

        frmRol.MdiParent = mdiPrincipal
        frmRol.frmParent = Me

        frmRol.modeForm = intMode
        frmRol.Text = strTitle
        frmRol.Show()

    End Sub

    Private Sub tsbConsultar_Click(ByVal sender As Object, ByVal e As EventArgs) Handles tsbConsultar.Click
        Me.tsbConsultar.Enabled = False
        Me.renderForm("Consultar - Rol", 2)
    End Sub

    Private Sub tsbBorrar_Click(ByVal sender As Object, ByVal e As EventArgs) Handles tsbBorrar.Click
        Dim objResult = MsgBox("Esta seguro que desea borrar el registro seleccionado?", MsgBoxStyle.YesNo, "Borrar - Rol")

        If (objResult = DialogResult.Yes) Then
            Dim strId As String = Me.dgvRol.Rows(Me.dgvRol.CurrentRow.Index).Cells(0).Value.ToString

            mdlCRUD.delRecord("rol", "rol_id = '" & strId & "'")

            If (mdlCRUD.blnFlag = True) Then
                Me.refreshList()
            End If
        End If
    End Sub

End Class